﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using VijayGande.WebParts.Classes;

namespace VijayGande.WebParts.DocumentsHyperLinksByChoiceViewWP
{
    [ToolboxItemAttribute(false)]
    public class DocumentsHyperLinksByChoiceViewWP : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/VijayGande.WebParts/DocumentsHyperLinksByChoiceViewWP/DocumentsHyperLinksByChoiceViewWPUserControl.ascx";

        string documentLibraryDisplayName = string.Empty;
        string documentLibraryInternalName = string.Empty;
        string documentLibraryWebUrl = string.Empty;
        string choiceFieldName = string.Empty;
        Label lblError = new Label();

        [WebBrowsable(true)]
        [WebDisplayName("Document Library Display Name")]
        [WebDescription("Enter the document library display name as it appears in the browser/UI. (e.g.Sales Resources)")]
        [Personalizable(PersonalizationScope.Shared)]
        public string DocumentLibraryDisplayName
        {
            get
            {
                return documentLibraryDisplayName;
            }
            set
            {
                documentLibraryDisplayName = value;
            }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Document Library Internal Name")]
        [WebDescription("Enter the document library internal name as it appears in the url, entered while crating the documnet library. (e.g.SalesReso)")]
        [Personalizable(PersonalizationScope.Shared)]
        public string DocumentLibraryInternalName
        {
            get
            {
                return documentLibraryInternalName;
            }
            set
            {
                documentLibraryInternalName = value;
            }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Document library web site name")]
        [WebDescription("Enter the web site name which contains the document library (e.g., 'http://SP2010:1000/MainWeb/SalesSubWeb')")]
        [Personalizable(PersonalizationScope.Shared)]
        public string DocumentLibraryWebURL
        {
            get
            {
                return documentLibraryWebUrl;
            }
            set
            {
                documentLibraryWebUrl = value;
            }
        }

        [WebBrowsable(true)]
        [WebDisplayName("Choice Field name")]
        [WebDescription("Enter the Choice Field name Value. (e.g., DocumentType)")]
        [Personalizable(PersonalizationScope.Shared)]
        public string ChoiceFieldName
        {
            get
            {
                return choiceFieldName;
            }
            set
            {
                choiceFieldName = value;
            }
        }

        protected override void CreateChildControls()
        {
            //this.ChromeType = PartChromeType.None;

            base.CreateChildControls();
            try
            {
                if (ValidateInitialValues())
                {
                    // Loads a user control
                    DocumentsHyperLinksByChoiceViewWPUserControl documentsHyperLinksWPUserControl = (DocumentsHyperLinksByChoiceViewWPUserControl)Page.LoadControl(_ascxPath);
                    documentsHyperLinksWPUserControl.docLibDispName = this.documentLibraryDisplayName;
                    documentsHyperLinksWPUserControl.docLibInternalName = this.documentLibraryInternalName;
                    documentsHyperLinksWPUserControl.choiceFieldName = this.choiceFieldName;
                    documentsHyperLinksWPUserControl.docLibraryWebSiteURL = this.documentLibraryWebUrl;
                    // Adds it to the controls collection of the Web Part
                    this.Controls.Add(documentsHyperLinksWPUserControl);
                }
                else
                {
                    this.Controls.Add(lblError);
                    return;
                }
            }
            catch (Exception ex)
            {
                Helper.ShowMessage(lblError, "Could not load User Control, Error Msg: <br />" + ex.Message + "<br/>" + "Stack Trace: " + ex.StackTrace);
                this.Controls.Add(lblError);
            }
        }

        private bool ValidateInitialValues()
        {
            bool bFlag = true;
            Helper.ShowMessage(lblError, "<b>Web Part Summary, incomplete:</b></br>This web part displays hyperlinks to the documents of a document library, The OOTB document library view web part  can only bring the documents from the document library of the current web site, where as this web part can bring the documents from other web sites.  User has to set the document library display name, internal name and web site url in custom properties sections.</br>");

            if (documentLibraryDisplayName.Equals(String.Empty))
            {
                Helper.ShowMessage(lblError, "Set the web part custom properties for document library name.");
                bFlag = false;
            }
            if (documentLibraryInternalName.Equals(String.Empty))
            {
                Helper.ShowMessage(lblError, "Set the web part custom properties for document library internal name, its being used to build the product specification url dynamically.");
                bFlag = false;
            }
            if (ChoiceFieldName.Equals(String.Empty))
            {
                Helper.ShowMessage(lblError, "Set the web part custom properties for ChoiceFieldName, its being used to get values into category drop down.");
                bFlag = false;
            }
            if (documentLibraryWebUrl.Equals(String.Empty))
            {
                Helper.ShowMessage(lblError, "Enter the web site name which contains the document library (e.g., 'http://SP2010:1000/MainWeb/SalesSubWeb')");
                bFlag = false;
            }
            return bFlag;
        }
    }
}